package hn.sigit.view;

import java.util.ArrayList;
import java.util.List;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;

import hn.sigit.dao.hnd.administrative.HND_NaturalPersonDAO;
import hn.sigit.model.hnd.administrative.HND_NaturalPerson;

import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.AutoCreate;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;

@Name("personHelper")
@Scope(value = ScopeType.CONVERSATION)
@AutoCreate
public class NatPersonHelper extends MantHelper<HND_NaturalPerson> {
	private static final long serialVersionUID = 1L;
	
	protected final String BY_NATIONAL_ID = "0";
	protected final String BY_RTN = "1";
	protected final String BY_LASTNAMES = "2";
	protected final String BY_FIRSTNAMES = "3";
	

	@Override
	public String addNew() {
		setSelected(new HND_NaturalPerson());
		editing = true;
		adding = true;
		
		return null;
	}
	
	@Override
	public String delete(HND_NaturalPerson np) {
		HND_NaturalPersonDAO.delete(np);
		entityList.remove(np);
		
		return null;
	}

	@Override
	public String select(HND_NaturalPerson np) {
		setSelected(np);
		super.doSelection();
		
		return null;
	}

	@Override
	public String saveSelected() {
		getSelected().setIdentity(getSelected().getIdentity().replace("-", ""));
		HND_NaturalPersonDAO.save(getSelected());
		if (adding) {
			getEntityList().clear();
			getEntityList().add(getSelected());
		}
		
		return null;
	}
	
	
	@Override
	public String quickSearch() {
		searchText = searchText.trim();
		List<HND_NaturalPerson> searchResultList = new ArrayList<HND_NaturalPerson>();
		
		if (searchSelector.equals(BY_NATIONAL_ID)) {
			searchText = searchText.replaceAll("-", "");
			searchResultList = wrapSingle(HND_NaturalPersonDAO.loadNatPersonPartyByIdentity(searchText)); 
		}
		else if (searchSelector.equals(BY_RTN)) {
			searchText = searchText.replaceAll("-", "");
			searchResultList = wrapSingle(HND_NaturalPersonDAO.loadNatPersonPartyByRTN(searchText)); 
		}
		else if (searchSelector.equals(BY_LASTNAMES))
			searchResultList = HND_NaturalPersonDAO.loadNatPersonPartiesByLastNames(searchText);
		else if (searchSelector.equals(BY_FIRSTNAMES))
			searchResultList = HND_NaturalPersonDAO.loadNatPersonPartiesByFirstNames(searchText);
		
		if (searchResultList.size() == 0) {
			FacesContext.getCurrentInstance().addMessage("",
					new FacesMessage(
							FacesMessage.SEVERITY_INFO,
							resBundle.loadMessage("txt.no_search_results"),
							""
					)
			);
		}
		
		setEntityList(searchResultList);
		
		return null;
	}
	
	
	public String names(HND_NaturalPerson natPerson) {
		StringBuffer sb = new StringBuffer();
		if (natPerson.getFirstName1() != null && natPerson.getFirstName1().trim().length() > 0)
			sb.append(natPerson.getFirstName1());
		
		if (natPerson.getFirstName2() != null && natPerson.getFirstName2().trim().length() > 0) {
			if (sb.length() > 0) sb.append(", ");
			sb.append(natPerson.getFirstName2());
		}
		
		return sb.toString();
	}

	public String lastNames(HND_NaturalPerson natPerson) {
		StringBuffer sb = new StringBuffer();
		if (natPerson.getLastName1() != null && natPerson.getLastName1().trim().length() > 0)
			sb.append(natPerson.getLastName1());
		
		if (natPerson.getLastName2() != null && natPerson.getLastName2().trim().length() > 0) {
			if (sb.length() > 0) sb.append(", ");
			sb.append(natPerson.getLastName2());
		}

		return sb.toString();
	}
	
	public String gender(HND_NaturalPerson natPerson) {
		if (natPerson.getGender() == 'M')
			return "Masculino";
		else if (natPerson.getGender() == 'F')
			return "Femenino";
		else
			return "Otro";
	}

	
	
	public String getBY_NATIONAL_ID() {
		return BY_NATIONAL_ID;
	}
	public String getBY_RTN() {
		return BY_RTN;
	}
	public String getBY_LASTNAMES() {
		return BY_LASTNAMES;
	}
	public String getBY_FIRSTNAMES() {
		return BY_FIRSTNAMES;
	}
}
